// component/navbar/navbar.js
import urls from '../../utils/urls.js';
import loginFn from '../../utils/login.js';
var app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    navbar: {
      type: null,
      value: [],

    },
    num: {
      type: Number,
      value: 0
    },
    optionsid: {
      type: String,
      value: ''
    },
    likeStatus: {
      type: Boolean,
      value: false,
    },
    wantStatus: {
      type: Boolean,
      value: false,
    },
    pageType: {
      type: String,
      value: 'class'
    },
    uid: {
      type: String,
      value: ''
    },
    name: {
      type: String,
      value: ''
    },
    avatar: {
      type: String,
      value: ''
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    heart: true,
    sharetip: false, //分享弹窗

  },

  /**
   * 组件的方法列表
   */
  methods: {
    //在线咨询
    goChat: function() {
      let url = '/pages/chat/chat?uid=' + this.data.uid+'&name='+this.data.name+'&avatar='+this.data.avatar;
      var pageType = this.data.pageType;
      var optionsid = this.data.optionsid;
      if (pageType == 'teacher') {
        url += '&teacherId='+optionsid;
      } else if (pageType == 'school') {
        url += '&schoolId='+optionsid;
      }
      wx.navigateTo({
        url: url
      })
    },

    //想学
    want: function() {
      console.log('want', this.data)
      var pageType = this.data.pageType;
      var postData = loginFn();
      var optionsid = this.data.optionsid;
      var url = urls.courserwant;
      if (pageType == 'teacher') {
        postData.teacherId = optionsid;
        url = urls.teacherwant;
      } else if (pageType == 'school') {
        postData.schoolId = optionsid;
        url = urls.schoolwant;
      } else {
        postData.courseId = optionsid;
      }

      let that = this;
      wx.request({
        url: url,
        method: 'GET',
        data: postData,
        success: function(res) {
          if (res.data.code == '0') {
            let wantStatus = that.data.wantStatus;
            that.setData({
              wantStatus: !wantStatus
            })

            wx.showToast({
              title: '操作成功'
            });
          }

        }
      })
    },

    //点赞
    reward: function() {
      console.log('reward', this.data)
      var pageType = this.data.pageType;
      var postData = loginFn();
      var optionsid = this.data.optionsid;
      var url = urls.likeCourse;
      if (pageType == 'teacher') {
        url = urls.teacherlike;
        postData.teacherId = optionsid;
      } else if (pageType == 'school') {
        url = urls.schoolLike;
        postData.schoolId = optionsid;
      } else {
        postData.remarkId = optionsid;
      }

      let that = this;
      wx.request({
        url: url,
        method: 'GET',
        data: postData,
        success: function(res) {
          if (res.data.code == '0') {
            let likeStatus = that.data.likeStatus;
            let num = that.data.num;
            if (likeStatus) {
              num--;
            } else {
              num++;
            }

            if(num < 0) {
              num = 0;
            }
            that.setData({
              likeStatus: !likeStatus,
              num: num
            })

            if (pageType == 'teacher') {
              wx.navigateTo({
                url: '/pages/reward/index?number=' + res.data.data.likeCount+'&teacherId='+optionsid,
              })
            } else {
              wx.showToast({
                title: '成功'
              });
            }


          }

        }
      })

    },
    // share: function () {
    //   wx.navigateTo({
    //     url: '/pages/share/index',
    //   })
    // },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function(res) {
      console.log(res);
      var pages = getCurrentPages()
      console.log(pages);
      console.log('aaa');
      this.setData({
        sharetip: false
      })

    },
    shareFn: function() { //分享弹框
      this.setData({
        sharetip: true
      })
    },
    shareCircle: function() { //分享到朋友圈
      wx.navigateTo({
        url: '/pages/share/index?type='+this.data.pageType+'&from=main',
      })
    },
    //隐藏mask
    hideMask() {
      this.setData({
        sharetip: false
      })
    }
  }
})